﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Infrastructure;
using Enterprise.Model.Note;
using Enterprise.Service.Note;
namespace Enterprise.UI.Web.Modules.Notice
{
    public partial class Index : System.Web.UI.Page
    {
        protected int year=(int)Utility.sink("year", Utility.MethodType.Get,0,0, Utility.DataType.Int);
        protected int month = (int)Utility.sink("month", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        string action = (string)Utility.sink("action", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (year == 0 || month == 0)
            {
                year = DateTime.Now.Year;
                month = DateTime.Now.Month;
            }
            else
            {
                switch (action)
                {
                    case "pre":
                        if (month == 1)
                        {
                            year = year - 1;
                            month = 12;
                        }
                        else
                        {
                            month = month - 1;
                        }
                        break;
                    case "next":
                        if (month == 12)
                        {
                            year = year + 1;
                            month = 1;
                        }
                        else
                        {
                            month = month + 1;
                        }
                        break;
                }
            }
            if (!IsPostBack)
            {
                OnStart();
            }
        }

        private void OnStart()
        {
            //NoteService nService = new NoteService();
            //var list = nService.NoteList().Where(p => p.NUSERID == Utility.Get_UserId);
            //GridView1.DataSource = list;
            //GridView1.DataBind();
        }

        protected string createCalendar()
        {
            int days = DateTime.DaysInMonth(year, month);//得到当月的天数  
            DateTime dt = new DateTime(year, month, 1);
            int weeke = Convert.ToInt32(dt.DayOfWeek.ToString("d"));//得到当月的第一天是星期几  
            string[,] cal = new string[7, 6];//创建一个二维数组来存放日期  
            for (int day = 1; day <= days; day++)
            {
                int x = (day + weeke - 1) % 7;//根据星期的规律循环x轴坐标  
                int y = (day + weeke - 1) / 7;//根据（月/星期）循环Y轴坐标  
                for (int j = 0; j < 6; j++)
                {
                    for (int i = 0; i < 7; i++)
                    {
                        cal[x, y] = day.ToString();//开始赋值  
                    }
                }
            }
            //输出  
            NoteService nService = new NoteService();
            System.Text.StringBuilder st = new System.Text.StringBuilder();
            st.Append("<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" height=\"500px\">");
            st.Append("<tr class=\"week_title\"><th> 星期日</th><th>星期一</th><th>星期二</th><th> 星期三</th><th>星期四</th><th>星期五</th><th> 星期六</th></tr>");
            for (int i = 0; i < 6; i++)
            {
                st.Append("<tr>");
                for (int j = 0; j < 7; j++)
                {
                    st.Append("<td class=\"weekday\"");
                    st.Append("width=\"13%\" align=\"left\" valign=\"top\" id=\"td" + i + j + "\" onMouseOver=\"fEvent('mouseover',this)\" onFocus=\"fEvent('focus',this)\" onBlur=\"fEvent('blur',this)\" >");
                    st.Append("<span class=\"month_date_num\">" + cal[j, i] + "<div id=\"uNotice\" class=\"notice\">" + GetNotice(nService, year, month, cal[j, i], "td" + i + j) + "</div></span></td>");//+cal[j, i] == "null"? "" : GetNotice(nService, Convert.ToDateTime(year + "/" + month + "/" + cal[j, i])) +
                }
                st.Append("</tr>");
            }
            st.Append("</table>");
            return st.ToString();
        }

        private string GetNotice(NoteService nService,int year,int month,string day,string tdId)
        {
            if (!string.IsNullOrEmpty(day))
            {
                DateTime dt = Convert.ToDateTime(year + "/" + month + "/" + day);
                string n = "<ul style=\"height:90px; width:100%;\">";
                var list = nService.NoteList().Where(p => p.NUSERID == Utility.Get_UserId && Convert.ToDateTime(p.NCREATETIME.ToShortDateString()) == dt).ToList();
                foreach (var var in list)
                {
                    n += "<li onclick='opdg(\"个人备忘\",\"/Modules/Notice/Notices.aspx?dt="+dt.ToShortDateString()+"\")' style=\"cursor:pointer;\">" + var.NTITLE + "</li>";
                }
                n += "<li onclick='opdg(\"新建个人备忘\",\"/Modules/Notice/NewNotice.aspx?dt=" + dt.ToShortDateString() + "\")' id=\"" + tdId + "li\" class=\"newNote\">新建备忘...</li>";
                n += "</ul>";
                return n;
            }
            else
            {
                return null;
            }            
        }

    }
}